package D150;

public class isPalindrome {
    public static void main(String[] args) {
        String s = "0P";
        System.out.println(new isPalindrome().isPalindrome2(s));
    }
    public boolean isPalindrome(String s) {
        String newStr = "";
        for (int i = 0; i < s.length(); i++) {
            if(Character.isDigit(s.charAt(i))){
                newStr += s.charAt(i);
            } else if (Character.isUpperCase(s.charAt(i))) {
                newStr += Character.toLowerCase(s.charAt(i));
            } else if (Character.isLowerCase(s.charAt(i))) {
                newStr += s.charAt(i);
            }else {
                continue;
            }
        }
        int left = 0,right = newStr.length() - 1;
        while (left < right){
            if(newStr.charAt(left) != newStr.charAt(right)){
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
    public boolean isPalindrome2(String s) {
        int n = s.length();
        int left = 0, right = n - 1;
        while (left < right) {
            while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
                ++left;
            }
            while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
                --right;
            }
            if (left < right) {
                if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
                    return false;
                }
                ++left;
                --right;
            }
        }
        return true;

    }
}
